<?php

if (!defined('IN_SYS'))
    exit('Access Denied');

class tags_controller extends admincp {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $yuming_id = $this->currentYumingId;
        if (!$yuming_id) {
            self::redirect("?m=yuming&a=select");
        }
        $where = "";
        $tagsname = gp("tagsname");
        if ($tagsname) {
            $where = " where tagsname like '%$tagsname%' and yuming_id='" . $this->currentYumingId . "'";
            $pg = new page("select * from tagslist_{$yuming_id} $where order by id desc");
        } else {
            $pg = new page("select * from tagslist_{$yuming_id} where  yuming_id='" . $this->currentYumingId . "' order by id desc");
        }

        $list = $pg->get_list(20);
        $page = $pg->get_page();
        require self::tpl('tags');
    }

    public function del() {
        $yuming_id = gp("yuming_id");
        if (!$yuming_id) {
            self::redirect("?m=yuming&a=select");
        }
        $id = gp('id');
        db::query("delete from tagslist_$yuming_id where id='$id' ");
        self::redirect('?m=tags&yuming_id=' . $yuming_id, "删除成功");
    }

    public function tags_add() {
        exit;
        $yuming_id = $this->currentYumingId;
        if (!$yuming_id) {
            self::redirect("?m=yuming&a=select");
        }
        if (IS_POST) {
            $msg = "";
            $v = trim(gp('tagsname'));
            if (!empty($v)) {
                $count = db::getField("select count(*) from tagslist_{$yuming_id} where tagsname='$v'");
                if ($count == 0) {
                    $addtime = time();
                    db::query("insert into tagslist_{$yuming_id} (tagsname,addtime,yuming_id) values ('$v','$addtime','$yuming_id')");
                    $msg = "添加成功";
                } else {
                    self::json(array('status' => 0, 'errMsg' => '标签已存在'));
                }
            } else {
                self::json(array('status' => 0, 'errMsg' => '请填写标签'));
            }
            self::json(array('status' => 1, 'msg' => $msg, 'redirect' => '?m=tags&a=index'));
        } else {
            require self::tpl('tags_add');
        }
    }

    public function edit() {
        $yuming_id = $this->currentYumingId;
        if (!$yuming_id) {
            self::redirect("?m=yuming&a=select");
        }
        $id = gp('id');
        $r = db::find("select * from tagslist_{$yuming_id} where id='$id' ");
        if (IS_POST) {
            $data = gp("tagsname");
            if (empty($data)) {
                self::json(array('status' => 0, 'errMsg' => '请填写标签'));
            }
            db::query("update tagslist_{$yuming_id} set tagsname='$data' where id='$id' ");
            self::json(array('status' => 1, 'msg' => '修改成功', 'redirect' => '?m=tags&a=index'));
        } else {
            require self::tpl("tags_edit");
        }
    }

    //导入词库
    public function tags_upload() {
        exit;
        $time = time();
        $yuming_id = $this->currentYumingId;
        if(empty($yuming_id)) {
            $this->respone_json('请先选择站点！');
        }
        $config = array(
            "pathFormat" => "/data/temp/{time}{rand:6}",
            "maxSize" => 1024 * 1024 * 1024 * 10,
            "allowFiles" => array('.txt'),
        );
        $up = new uploader('file', $config, 'upload');
        $data = $up->getFileInfo();
        if (empty($data['url'])) {
            $this->respone_json($data['state']);
        }
        
        $file = ROOT . $data['url'];
        $content = file_get_contents($file);
        unlink($file);
        $encode = mb_detect_encoding($content, array("ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5'));
        $content = mb_convert_encoding($content, 'UTF-8', $encode);
        $array = explode(",", $content);
        if (count($array) > 200) {
            $this->respone_json('标签个数必须是200以内！');
        }
        $suc_count = 0;
        foreach ($array as $v) {
            try {
                $count = db::getField("select count(*) from tagslist_{$yuming_id} where tagsname='$v'");
                if ($count == 0) {
                    $addtime = time();
                    db::query("insert into tagslist_{$yuming_id} (tagsname,addtime,yuming_id) values ('$v','$addtime','$yuming_id')");
                    $suc_count++;
                }
            } catch (Exception $ex) {
                continue;
            }
        }
        $this->respone_json(['count'=>$suc_count]);
    }

}
